//
//  myView.m
//  切换页面
//
//  Created by chenlishuang on 16/8/1.
//  Copyright © 2016年 chenlishuang. All rights reserved.
//

#import "MyView.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@implementation MyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self allViews];
    }
    return self;
}
- (void)allViews{
    //UIScrollView
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.scrollView.contentSize = CGSizeMake(self.bounds.size.width*3, self.bounds.size.height*2);
    //设置整屏滚动
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self addSubview:self.scrollView];
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth , 70)];
    self.topView.backgroundColor = [UIColor yellowColor];
    [self addSubview:self.topView];
    //添加背景
    self.view = [[UIView alloc]initWithFrame:CGRectMake(0, 10, kScreenWidth/3, 50)];
    self.view.backgroundColor = [UIColor magentaColor];
    self.view.layer.masksToBounds = YES;
    self.view.layer.cornerRadius = 20;
    [self.topView addSubview:self.view];
    //添加button
    for (int i = 0; i<3; i++) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor colorWithRed:arc4random()%255/256.0 green:arc4random()%255/256.0 blue:arc4random()%255/256.0 alpha:1];
        view.frame = CGRectMake(i*kScreenWidth, 0, kScreenWidth, kScreenHeight-64);
        [self.scrollView addSubview:view];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        switch (i) {
            case 0:
                [button setTitle:@"第一页" forState:UIControlStateNormal];
                break;
            case 1:
                [button setTitle:@"第二页" forState:UIControlStateNormal];
                break;
            case 2:
                [button setTitle:@"第三页" forState:UIControlStateNormal];
                break;
            default:
                break;
        }
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 100+i;
        button.frame = CGRectMake(i*kScreenWidth/3, 0, kScreenWidth/3, 60);
        [self.topView addSubview:button];
    }

}
- (void)buttonClick:(UIButton *)sender{
    if (sender.tag == 100) {
        NSLog(@"1");
        [UIView animateWithDuration:0.5 animations:^{
            self.view.frame = CGRectMake(0, 10, kScreenWidth/3, 50);
        }];
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }
    if (sender.tag == 101) {
        NSLog(@"2");
        [UIView animateWithDuration:0.5 animations:^{
            self.view.frame = CGRectMake(kScreenWidth/3, 10, kScreenWidth/3, 50);
        }];
        self.scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
    }
    if (sender.tag == 102) {
        NSLog(@"3");
        [UIView animateWithDuration:0.5 animations:^{
            self.view.frame = CGRectMake(kScreenWidth/3*2, 10, kScreenWidth/3, 50);
        }];
        self.scrollView.contentOffset = CGPointMake(kScreenWidth*2, 0);
    }
}
@end
